package com.example.partner.adapter.team;

import android.content.Context;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.partner.R;
import com.example.partner.model.vo.TeamUserVo;
import com.google.android.flexbox.FlexboxLayout;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.List;

public class TeamAdapter extends BaseAdapter {
    private Context context;
    private List<TeamUserVo> teamList;

    public TeamAdapter(Context context, List<TeamUserVo> teamList) {
        this.context = context;
        this.teamList = teamList;
    }

    @Override
    public int getCount() {
        return teamList.size();
    }

    @Override
    public Object getItem(int position) {
        return teamList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_container_team, parent, false);
            viewHolder.mButton = (Button) convertView.findViewById(R.id.more_team_info);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        ImageView avatarUrl = convertView.findViewById(R.id.avatarUrl);
        TextView teamName = convertView.findViewById(R.id.teamName);
        TextView teamNum=convertView.findViewById(R.id.teamNum);
        TextView teamPlace = convertView.findViewById(R.id.group_place);
        TextView teamDescription=convertView.findViewById(R.id.group_description);
        TeamUserVo team = teamList.get(position);
        teamNum.setText("队伍人数:"+team.getHasJoinNum()+"/"+team.getMaxNum());
        teamName.setText(team.getName());
        teamPlace.setText(team.getPlace());
        teamDescription.setText(team.getDescription());
        avatarUrl.setImageResource(R.mipmap.team_avatar);
        viewHolder.mButton.setOnClickListener(v -> mOnItemMoreInfoListener.onMoreInfoClick(position));
        if(team.getAvatarUrl()!=null){
            Glide.with(context).load(team.getAvatarUrl()).into(avatarUrl);
        }
        return convertView;
    }
    public interface onItemMoreInfoListener {//接口
        void onMoreInfoClick(int i);
    }

    private onItemMoreInfoListener mOnItemMoreInfoListener;

    public void setOnItemMoreInfoClickListener(onItemMoreInfoListener mOnItemMoreInfoListener) {//这个方法传入一个接口
        this.mOnItemMoreInfoListener = mOnItemMoreInfoListener;//用传入的接口初始化私有变量mOnItemMoreInfoListener
    }

    class ViewHolder {
        Button mButton;
    }
}
